package com.hxtec.polaris.com.hxtec.polaris.insertDb;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hxtec.polaris.entity.ShopCategory;
import com.hxtec.polaris.entity.ShopProductBase;
import com.hxtec.polaris.mapper.ShopCategoryMapper;
import com.hxtec.polaris.mapper.ShopProductBaseMapper;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Describtion 分类json入库
 * @Author yonyong
 * @Date 2019/11/11 16:47
 * @Version 1.0.0
 **/
@SpringBootTest
public class InsertGoods {

    @Resource
    private ShopProductBaseMapper shopProductBaseMapper;

    /**
     * 执行此方法    入库相关表 shop_category
     * @throws Exception
     */
    @Test
    public void insert() throws Exception {
        final ObjectMapper mapper = new ObjectMapper();
        String json = InsertCategory.readJsonData("json/category.json");
        List list = mapper.readValue(json,List.class);
        for (Object o :list){
            HashMap map = (HashMap) o;
            ShopProductBase shopProductBase = new ShopProductBase();

            String image = String.valueOf( map.get("image"));
            String title = String.valueOf( map.get("title"));
            String price = String.valueOf( map.get("price"));
            String sales = String.valueOf( map.get("sales"));

            if (null!=image && !image.equals("null")){
//                shopProductBase.set(image);
            }
            if (null!=title && !title.equals("null")){
                shopProductBase.setName(title);
            }
            if (null!=price && !price.equals("null")){
//                shopProductBase.setName(price);
            }
            if (null!=sales && !sales.equals("null")){
//                shopProductBase.setImage(sales);
            }

//            shopProductBase.setIsDelete("N");
            shopProductBase.setCreateTime(new Date());
            shopProductBase.setUpdateTime(new Date());

//            shopCategoryMapper.insert(shopCategory);
        }

        updateIsParent();

    }

    /**
     * 修正数据库is_parent状态
     */
    @Test
    public void updateIsParent(){
//        List<ShopCategory> list = shopCategoryMapper.selectAll();
//        for (ShopCategory s: list){
//            if (StringUtils.isNotBlank(s.getParentId())){
//                ShopCategory temp = shopCategoryMapper.selectByPrimaryKey(s.getParentId());
//                temp.setIsParent("Y");
//                shopCategoryMapper.updateByPrimaryKeySelective(temp);
//            }
//        }
    }

    @Test
    public void read(){
        System.out.println(InsertCategory.readJsonData("json/goods.json"));
    }
}
